//
//  JRAlertView.m
//  ikarosKit
//
//  Created by rui on 2024/10/17.
//

#import "JRAlertView.h"

//全局信号量
static dispatch_semaphore_t _jr_alert_view_globalInstancesLock = nil;
//执行QUEUE的Name
char *QUEUE_NAME = "com.djr.ikaros.alert.queue";

//初始化 -- 借鉴YYWebImage的写法
FOUNDATION_STATIC_INLINE void BaseAlertViewInitGlobal(void) {
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        _jr_alert_view_globalInstancesLock = dispatch_semaphore_create(1);
    });
}

@implementation JRAlertView

- (void)customInit {
    [super customInit];
    BaseAlertViewInitGlobal();
    self.isTouchDismiss = NO;
    self.backgroundColor = [[UIColor blackColor] colorWithAlphaComponent:0.6];
    self.frame = [[UIScreen mainScreen] bounds];
}

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
    CGPoint touchPoint = [[touches anyObject] locationInView:self];
    UIView *responderView = [self hitTest:touchPoint withEvent:event];
    if (responderView != self.containerView && responderView != nil && self.isTouchDismiss) {
        [self dismiss:YES completion:nil];
    }
}

- (void)show:(BOOL)animated completion:(nullable dispatch_block_t)completion {
    dispatch_async(dispatch_queue_create(QUEUE_NAME, DISPATCH_QUEUE_SERIAL), ^{
        dispatch_semaphore_wait(_jr_alert_view_globalInstancesLock, DISPATCH_TIME_FOREVER);
        dispatch_async(dispatch_get_main_queue(), ^{
            self.backgroundColor = [UIColor clearColor];
            self.autoresizingMask = UIViewAutoresizingFlexibleWidth|UIViewAutoresizingFlexibleHeight;
            [[UIApplication sharedApplication].delegate.window addSubview:self];
            self.alpha = 1;
            CGAffineTransform transform = CGAffineTransformScale(CGAffineTransformIdentity, 1.0, 1.0);
            self.containerView.transform = CGAffineTransformScale(CGAffineTransformIdentity, 0.2, 0.2);
            self.containerView.alpha = 0;
            if (animated) {
                [UIView animateWithDuration:0.35 delay:0.1 usingSpringWithDamping:0.75 initialSpringVelocity:7 options:UIViewAnimationOptionCurveEaseInOut animations:^{
                    self.backgroundColor = [[UIColor blackColor] colorWithAlphaComponent:0.4];
                    self.containerView.transform = transform;
                    self.containerView.alpha = 1;
                } completion:^(BOOL finished) {
                    !completion?:completion();
                }];
            } else {
                self.backgroundColor = [[UIColor blackColor] colorWithAlphaComponent:0.4];
                self.containerView.transform = transform;
                self.containerView.alpha = 1;
                !completion?:completion();
            }
        });
    });
}

- (void)dismiss:(BOOL)animated completion:(nullable dispatch_block_t)completion {
    dispatch_async(dispatch_queue_create(QUEUE_NAME, DISPATCH_QUEUE_SERIAL), ^{
        dispatch_semaphore_signal(_jr_alert_view_globalInstancesLock);
        dispatch_async(dispatch_get_main_queue(), ^{
            if (animated) [self _dismiss];
            else [self _dismissWithOutAnimation];
            !completion?:completion();
        });
    });
}

- (void)_dismiss {
    [UIView animateWithDuration:0.35 animations:^{
        self.alpha = .1;
    } completion:^(BOOL finished) {
        [self removeFromSuperview];
    }];
}

- (void)_dismissWithOutAnimation {
    [self removeFromSuperview];
}

#pragma mark - associated
- (JRBaseView *)containerView {
    if (_containerView == nil) {
        JRBaseView *view = JRBaseView.new;
        view.backgroundColor = UIColor.whiteColor;
        view.layer.cornerRadius = 8.0;
        [self addSubview:view];
        _containerView = view;
    } return _containerView;
}
@end
